@extends('frontend/layouts/bootstrap')

@section('content')
<h2>{{ $customer }}-giin bill</h2>

{{ Form::open(array('route' => array('car_balance.bill.create.step2'), 'class' => 'form-horizontal')) }}

<h4>Bill info</h4>
<table class="table table-bordered">
    <thead style="background-color: #f0f0f0;">
    <th title="Create date">Create date</th>
    <th title="Expriy date">Expriy date</th>
    <th title="Umnuh uldegdel">Umnuh uldegdel</th>
    </thead>
    <tr>
        <td>
            <input class="datepicker input-medium" placeholder="Create Date" title="Create Date" name="create_date" type="text">
        </td>
        <td>
            <input class="datepicker input-medium" placeholder="Expriy Date" title="Expriy Date" name="expiry_date" type="text">
        </td>
        <td>
            <input class="input-small" placeholder="Umnuh uldegdel" title="Umnuh uldegdel" name="umnuh_uldegdel" type="text" id="umnuh_uldegdel">
        </td>
    </tr>
</table>

@if ($cars->count())
<h4>Car Bill detail</h4>
<table class="table table-bordered" id="billDetailed">
    <thead style="background-color: #f0f0f0;">
    <th title="Date">日付</th>
    <th title="Auction">オーク会場</th>
    <th title="Auction no">出品番号</th>
    <th title="Car name">車種</th>
    <th title="Car year">年式</th>
    <th title="Chassis number">車体番号</th>
    <th style="text-align: center;" title="Bill price">購入額</th>
    <th style="text-align: center;" title="Vat percent">消費税</th>
    <th style="text-align: center;" title="Recycle amount">リサイクル料</th>
    <th style="text-align: center;" title="Auction commission">落札料</th>
    <th style="text-align: center;" title="Transport fee">Transport</th>
    <th style="text-align: center;" title="Vanning">Vanning</th>
    <th style="text-align: center;" title="Others">Others</th>
    <th style="text-align: center;" title="Commission">Commission</th>
    <th style="text-align: center;" title="Bill total amount">合計</th>
    <th title="Action">Action</th>
    </thead>
    @foreach($cars as $car)
    <tr>
        <td>{{ $car->purchase_date }}</td>
        <td>{{ $car->auction_name }}</td>
        <td>{{ $car->lot_no }}</td>
        <td>{{ $car->car_maker }} {{ $car->car_name }}</td>
        <td>{{ $car->car_year }}</td>
        <td>{{ $car->chasis_no }}</td>
        <td style="text-align: right;">{{ $car->bill_price ? number_format($car->bill_price, 0) : 0; }}</td>
        <td style="text-align: right;">{{ number_format(($car->bill_price*$car->bill_vat)/100, 0); }}</td>
        <td style="text-align: right;">{{ $car->bill_recycle ? number_format($car->bill_recycle, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_rakusatu ? number_format($car->bill_rakusatu, 0) : 0; }}</td>
        <td><input class="input-mini" placeholder="Bill Transport Fee" title="Bill Transport Fee" name="bill_transport_fee" id="bill_transport_fee_{{$car->id}}" type="text" value="{{ $car->bill_transport_fee }}" style="text-align: right;"></td>
        <td style="text-align: right;">{{ $car->bill_vanning ? number_format($car->bill_vanning, 0) : 0; }}</td>
        <td><input class="input-mini" placeholder="Bill Others" title="Bill Others" name="bill_others" id="bill_others_{{$car->id}}" type="text" value="{{ $car->bill_others }}" style="text-align: right;"></td>
        <td style="text-align: right;">{{ $car->bill_commission ? number_format($car->bill_commission, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_total_amount ? number_format($car->bill_total_amount, 0) : 0; }}</td>
        <td>
            <input style="width: 50px;" class="btn btn-mini" value="Save" onclick="setTransportOthers({{ $car->id }})">
        </td>
    </tr>
    @endforeach
    <tr>
        <td colspan="6">Total</td>
        <td style="text-align: right;">{{ $total_bill['bill_price'] ? number_format($total_bill['bill_price'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_vat'] ? number_format($total_bill['bill_vat'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_recycle'] ? number_format($total_bill['bill_recycle'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_rakusatu'] ? number_format($total_bill['bill_rakusatu'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_transport_fee'] ? number_format($total_bill['bill_transport_fee'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_vanning'] ? number_format($total_bill['bill_vanning'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_others'] ? number_format($total_bill['bill_others'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_commission'] ? number_format($total_bill['bill_commission'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['bill_total_amount'] ? number_format($total_bill['bill_total_amount'], 0) : 0; }}</td>
        <td style="text-align: right;"></td>
    </tr>
</table>
@endif

@if ($penalties1->count())
<h4>Tulbur udaasan penalty detial</h4>
<table class="table table-bordered" id="billDetailed">
    <thead style="background-color: #f0f0f0;">
    <th title="Penalty date">Penalty date</th>
    <th title="Chassis number">車体番号</th>
    <th style="text-align: center;" title="Penalty amount">Penalty amount</th>
    </thead>
    @foreach($penalties1 as $penalty)
    <tr>
        <td>{{ $penalty->penalty_date }}</td>
        <td>{{ $penalty->chasis_no }}</td>
        <td style="text-align: right;">{{ $penalty->penalty_amount ? number_format($penalty->penalty_amount, 0) : 0; }}</td>
    </tr>
    @endforeach
    <tr>
        <td colspan="2">Total</td>
        <td style="text-align: right;">{{ $total_bill['penalty_amount1'] ? number_format($total_bill['penalty_amount1'], 0) : 0; }}</td>
    </tr>
</table>
@endif

@if ($penalties2->count())
<h4>Auction penalty detial</h4>
<table class="table table-bordered" id="billDetailed">
    <thead style="background-color: #f0f0f0;">
    <th title="Penalty date">Penalty date</th>
    <th title="Chassis number">車体番号</th>
    <th style="text-align: center;" title="Penalty amount">Penalty amount</th>
    </thead>
    @foreach($penalties2 as $penalty)
    <tr>
        <td>{{ $penalty->penalty_date }}</td>
        <td>{{ $penalty->chasis_no }}</td>
        <td style="text-align: right;">{{ $penalty->penalty_amount ? number_format($penalty->penalty_amount, 0) : 0; }}</td>
    </tr>
    @endforeach
    <tr>
        <td colspan="2">Total</td>
        <td style="text-align: right;">{{ $total_bill['penalty_amount2'] ? number_format($total_bill['penalty_amount2'], 0) : 0; }}</td>
    </tr>
</table>
@endif

<h4>Total</h4>
<table class="table table-bordered" id="billTotalDetailed">
    <thead style="background-color: #f0f0f0;">
    <th style="text-align: center;" title="Bill total amount">Bill total amount</th>
    <th style="background-color: #f0f0f0;" title="Umnuh uldegdel">Umnuh uldegdel</th>
    <th style="text-align: center;" title="Penalty 1 amount">Penalty 1 amount</th>
    <th style="text-align: center;" title="Penalty 2 amount">Penalty 2 amount</th>
    </thead>
    <tr>
        <td style="text-align: right;">{{ $total_bill['bill_total_amount'] ? number_format($total_bill['bill_total_amount'], 0) : 0; }}</td>
        <td style="text-align: right;" id="umnuh_uldegdel_total"></td>
        <td style="text-align: right;">{{ $total_bill['penalty_amount1'] ? number_format($total_bill['penalty_amount1'], 0) : 0; }}</td>
        <td style="text-align: right;">{{ $total_bill['penalty_amount2'] ? number_format($total_bill['penalty_amount2'], 0) : 0; }}</td>
    </tr>
    <tr>
        <td colspan="3">Total</td>
        <td style="text-align: right;" id="bill_total">{{ ($total_bill['bill_total_amount'] + $total_bill['penalty_amount1']+ $total_bill['penalty_amount2']) ? number_format($total_bill['bill_total_amount'] + $total_bill['penalty_amount1']+ $total_bill['penalty_amount2'], 0) : 0; }}</td>
    </tr>
</table>

<button type="submit" class="btn btn-large btn-success"><i class="icon-plus icon-white"></i> Хадгалах</button> &nbsp;&nbsp;
{{  Form::close() }}
@stop


@section('pagestyles')
<link rel="stylesheet" type="text/css" href={{ asset('assets/chosen-bootstrap/chosen.min.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-daterangepicker/daterangepicker.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/lightbox/lightbox.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-datepicker/datepicker.css') }} />
<style type="text/css" media="screen">
    .container-wrapper {
        /* Attach fixed-th-table to this container,
           in order to layout fixed-th-table
           in the same way as scolled-td-table" */
        position: relative;

        /* Truncate fixed-th-table */
        overflow: hidden;
        width: 1170px;
        white-space: nowrap;
    }
    table thead td {
        font-weight: bold;
    }
    table tbody th {
        font-weight: normal !important;
    }

    .fixed-th-table-wrapper td,
    .fixed-th-table-wrapper th,
    .scrolled-td-table-wrapper td,
    .scrolled-td-table-wrapper th {
        /* Set background to non-transparent color
           because two tables are one above another.
         */
        background: white;
    }
    .fixed-th-table-wrapper {
        /* Make table out of flow */
        position: absolute;
    }
    .fixed-th-table-wrapper th {
        /* Place fixed-th-table th-cells above
           scrolled-td-table td-cells.
         */
        position: relative;
        z-index: 1;
    }
    .scrolled-td-table-wrapper td {
        /* Place scrolled-td-table td-cells
           above fixed-th-table.
         */
        position: relative;
    }
    .scrolled-td-table-wrapper {
        /* Make horizonal scrollbar if needed */
        overflow-x: scroll;
    }

</style>
@stop

@section('pagescripts')
<!--page specific plugin scripts-->
<script type="text/javascript" src={{ asset('assets/chosen-bootstrap/chosen.jquery.min.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/date.js') }} ></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/daterangepicker.js') }}></script>
<script type="text/javascript" src={{ asset('assets/lightbox/lightbox-2.6.min.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-datepicker/datepicker.js') }}></script>
<script type="text/javascript">
    $(function() {
        $( ".datepicker" ).datepicker({
            defaultDate: "+0d",
            format: "yyyy-mm-dd",
            autoclose: true
        });
    });
    $("#umnuh_uldegdel").each(function(i){
        $(this).change(function () {
            var bill_total = $('#bill_total').text().replace(/\,/g,'');
            var umnuh_uldegdel = document.getElementById("umnuh_uldegdel").value;
            var total = parseInt(umnuh_uldegdel) + parseInt(bill_total);
            $('#umnuh_uldegdel_total').text(numberWithCommas(umnuh_uldegdel));
            $('#bill_total').text(numberWithCommas(total));
        });
    });
    function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    $(document).ready(function() {
        function tally (selector) {
            $(selector).each(function () {
                var total = 0,
                    column = $(this).siblings(selector).andSelf().index(this);
                $(this).parents().prevUntil(':has(' + selector + ')').each(function () {
                    total += parseFloat($('td.bill_total_amount:eq(' + column + ')', this).html()) || 0;
                })
                $(this).html(total);
            });
        }
        tally('td.sub_total_bill_amount');
        var sum = 0;
        $(".bill_total_amount").each(function() {
            var value = $(this).text();
            if(!isNaN(value) && value.length != 0) {
                //sum += parseFloat(value);
                console.log(parseFloat(value));
            }
        });

        /*var total = 0;
        for ( var i = 0; i < {{$cars->count()}}; i++ ) {
           // total = total + parseInt($('.bill_total_amount').val());
            console.log(parseInt($('.bill_total_amount').val()));
        } console.log(total);*/
    });


    function setTransportOthers(carId) {
        var bill_transport = document.getElementById("bill_transport_fee_"+carId).value;
        var bill_others = document.getElementById("bill_others_"+carId).value;
        var request = $.ajax({
            url: "{{ route('car_balance.bill.edit_transport_other') }}",
            type: "get",
            data: {carId : carId, bill_transport: bill_transport, bill_others: bill_others},
            success: function(data){
                if (data.status = 'ok') {
                    window.location.reload(true);
                }
            }});
    }

</script>
@stop